from datetime import datetime
from . import db
from werkzeug.security import generate_password_hash, check_password_hash

class User(db.Model):
    id           = db.Column(db.Integer, primary_key=True)
    username     = db.Column(db.String(64), unique=True, nullable=False)
    full_name = db.Column(db.String(64), nullable=True)
    password_hash= db.Column(db.String(256), nullable=False)
    is_admin = db.Column(db.Boolean, default=False, nullable=False)  # 新增
    created_at   = db.Column(db.DateTime, default=datetime.utcnow)
    expiry_date = db.Column(db.Date, nullable=True)
    is_active = db.Column(db.Boolean, default=True, nullable=False)
    remarks = db.Column(db.String(256), nullable=True)

    def set_password(self, password: str):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password: str) -> bool:
        return check_password_hash(self.password_hash, password)

class Course(db.Model):
    id        = db.Column(db.Integer, primary_key=True)
    name      = db.Column(db.String(128), nullable=False)
    owner_id  = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    seq       = db.Column(db.Integer, nullable=False, default=0)  # 序号，用于上下移
    created_at= db.Column(db.DateTime, default=datetime.utcnow)

class School(db.Model):
    id         = db.Column(db.Integer, primary_key=True)
    name       = db.Column(db.String(128), nullable=False)
    owner_id   = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    seq        = db.Column(db.Integer, nullable=False, default=0)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

class Grade(db.Model):
    id         = db.Column(db.Integer, primary_key=True)
    name       = db.Column(db.String(128), nullable=False)
    owner_id   = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    seq        = db.Column(db.Integer, nullable=False, default=0)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

class Schedule(db.Model):
    id            = db.Column(db.Integer, primary_key=True)
    user_id       = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    student_name  = db.Column(db.String(128), nullable=False)
    course        = db.Column(db.String(128), nullable=False)
    school        = db.Column(db.String(128), nullable=False)
    grade         = db.Column(db.String(64), nullable=False)
    address       = db.Column(db.String(256), nullable=False)
    fee           = db.Column(db.Numeric(10,2), nullable=False)
    type          = db.Column(db.String(16), nullable=False)  # 'once' or 'weekly'
    one_date      = db.Column(db.Date, nullable=True)
    weekdays      = db.Column(db.String(32), nullable=True)   # CSV 如 "Mon,Wed"
    start_date    = db.Column(db.Date, nullable=True)
    end_date      = db.Column(db.Date, nullable=True)
    start_time    = db.Column(db.Time, nullable=False)
    end_time      = db.Column(db.Time, nullable=False)
    remark        = db.Column(db.String(256), nullable=True)
    created_at    = db.Column(db.DateTime, default=datetime.utcnow)
    status = db.Column(db.String(16), nullable=False, default='未上')